package tree

// https://leetcode-cn.com/problems/binary-tree-right-side-view/
// 二叉树的右视图
// 算法: 依次遍历每一层
// 时间复杂度: O(n)
func RightSideView(root *TreeNode) []int {
	retArr := make([]int, 0)
	if nil == root {
		return retArr
	}

	nodeRecurArr := make([]*TreeNode, 0)
	nodeRecurArr = append(nodeRecurArr, root)
	for {
		isAllNull := true
		var rightVal int
		currentLevelNodeArr := make([]*TreeNode, 0);
		for _, currentNode := range nodeRecurArr {
			if nil != currentNode {
				rightVal = currentNode.Val
				currentLevelNodeArr = append(currentLevelNodeArr, currentNode.Left, currentNode.Right)
				isAllNull = false
			}
		}
		if isAllNull {
			break
		}
		retArr = append(retArr, rightVal)
		nodeRecurArr = currentLevelNodeArr
	}

	return retArr
}